package com.sunthy.mall.controller.client;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.sunthy.mall.model.admin.Result;
import com.sunthy.mall.model.admin.bo.client.order.AskGoodsMsgBo;
import com.sunthy.mall.model.admin.vo.goods.AllGoodsVo;
import com.sunthy.mall.model.admin.vo.client.goods.AllReplyForGoodsIdVo;
import com.sunthy.mall.model.admin.vo.client.goods.GoodsCommentVo;
import com.sunthy.mall.model.admin.vo.client.goods.GoodsInfoForUserVo;
import com.sunthy.mall.service.admin.goods.GoodsService;
import com.sunthy.mall.service.admin.goods.GoodsServiceImpl;
import com.sunthy.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/goods/*")
public class GoodsServlet extends HttpServlet {
    private Gson gson = new Gson();

    private GoodsService goodsService = new GoodsServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/askGoodsMsg".equals(pathInfo)) {
            askGoodsMsg(request, response);
        }
    }
    /**
     * 留言的逻辑:
     *      1.接收请求参数
     *      2.添加数据到数据库
     *      3.返回结果
     * @param request
     * @param response
     */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        AskGoodsMsgBo askGoodsMsgBo = null;
        // 校验参数
        try {
            askGoodsMsgBo = gson.fromJson(requestBody,AskGoodsMsgBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 添加到数据路
        int code = goodsService.addAskGoodsMsg(askGoodsMsgBo);
        // 返回结果
        if (code==0){
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("发送异常")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/getGoodsByType".equals(pathInfo)) {
            getGoodsByType(request, response);
        } else if ("/getGoodsMsg".equals(pathInfo)) {
            getGoodsMsg(request, response);
        } else if ("/getGoodsComment".equals(pathInfo)) {
            getGoodsComment(request, response);
        } else if ("/getGoodsInfo".equals(pathInfo)) {
            getGoodsInfo(request, response);
        } else if ("/searchGoods".equals(pathInfo)) {
            searchGoods(request, response);
        }
    }

    /**
     * 关键词搜索所有goods的逻辑:
     * 1.接受请求参数
     * 2.搜索数据库
     * 3.返回参数
     *
     * @param request
     * @param response
     */
    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String keyword = request.getParameter("keyword");
        // 校验参数
        // 查询数据库
        List<AllGoodsVo> allGoodsBos = goodsService.getGoodsByName(keyword);
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok(allGoodsBos)));
    }

    /***********
     * 回显的逻辑：
     *  1.接收请求参数
     *  2.查询数据库
     *  3.返回结果
     * @param request
     * @param response
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        // 查询数据库
        GoodsInfoForUserVo goodsInfoForUserVo = goodsService.getGoodsInfoForUserById(id);
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok(goodsInfoForUserVo)));
    }

    /**
     * 获取商品评价的逻辑:
     * 1.接收请求参数
     * 2.查询数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String goodsIdStr = request.getParameter("goodsId");
        // 校验参数
        Integer goodsId = null;
        try {
            goodsId = Integer.parseInt(goodsIdStr);
        } catch (NumberFormatException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 查询数据库
        GoodsCommentVo goodsCommentVo = goodsService.getGoodsComment(goodsId);
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok(goodsCommentVo)));
    }

    /**
     * 获得商品详情页问答区数据
     * 获取商品Msg信息的逻辑
     * 1.接收请求参数
     * 2.查询数据库
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String idStr = request.getParameter("id");
        Integer id = null;
        // 校验参数
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            response.getWriter().println(gson.toJson(Result.error("参数异常")));
            return;
        }
        // 查询数据库
        List<AllReplyForGoodsIdVo> allReplyForGoodsIdVo = goodsService.getGoodsMsgById(id);
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok(allReplyForGoodsIdVo)));
    }

    /**
     * 获取所有给定类型商品的逻辑:
     * 1.接收参数  (-1)表示获取所有类型的商品
     * 2.查询数据库
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String typeIdStr = request.getParameter("typeId");
        int typeId = Integer.parseInt(typeIdStr);
        // 查询数据库
        List<AllGoodsVo> allGoodsList = goodsService.allGoodsByTypeId(typeId);
        // 返回結果
        response.getWriter().println(gson.toJson(Result.ok(allGoodsList)));
    }
}
